package com.ht.psplan;

import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Yan Xue
 *         Date:2009-6-4
 */

public class SearchHistory {
    private PlanPersistLayer sl = PlanPlFactory.getPersistLayer();
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat formater = new SimpleDateFormat("yyyy");

    public List<String> getInfo(PlanCommInfo p, Date date) throws ParseException, RemoteException {
        List<Long> list;
        list = sl.getPlanExist(p, formater.format(date));
        Collections.sort(list);
        List<String> result = new ArrayList<String>();
        int count = 0;
        for (int i = 1; i < list.size(); i++) {
            long pre = list.get(i - 1);
            long l = list.get(i);
            if (l - pre > 24 * 60 * 60 * 1000) {
                result.add(format.format(list.get(count)) + " ~ " + format.format(pre));
                count = i;
            }
            if (i == list.size() - 1) {
                result.add(format.format(list.get(count)) + " ~ " + format.format(list.get(i)));
            }
        }
        return result;
    }

    public List<String> getSentPlanInfo(PlanCommInfo p, Date date) throws ParseException, RemoteException {
        List<Long> list;
        list = sl.getSentPlan(p, formater.format(date));
        Collections.sort(list);
        List<String> result = new ArrayList<String>();
        int count = 0;
        for (int i = 1; i < list.size(); i++) {
            long pre = list.get(i - 1);
            long l = list.get(i);
            if (l - pre > 24 * 60 * 60 * 1000) {
                result.add(format.format(list.get(count)) + " ~ " + format.format(pre));
                count = i;
            }
            if (i == list.size() - 1) {
                result.add(format.format(list.get(count)) + " ~ " + format.format(list.get(i)));
            }
        }
        return result;
    }

    public List<String> getNoSentPlanInfo(PlanCommInfo p) throws ParseException, RemoteException {
        List<String> result = new ArrayList<String>();
        Map<Date,Date> map = sl.getNoSentPlan(p);
        if(map == null)return result;
        Object[] objects = map.keySet().toArray();
        Arrays.sort(objects);
        for (Object o : objects) {
            Date startTime = (Date) o;
            Date endTime = map.get(o);
            result.add(format.format(startTime)+ " ~ " + format.format(endTime));
        }
        return result;
    }
}
